/*
 * MusicLabs, Copyright (C) 2008-2009 Felippe Ragghianti
 * http://code.google.com/p/musiclabs
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 */


package br.com.music.utils;

import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.SynthesizerModeDesc;
import javax.speech.synthesis.Voice;

/**
 * Music Labs Logger.
 *
 * This class is meant to be used in the whole code as to generate track of
 * execution in several levels: info, warning, error, trace, debug.
 *
 * @note Please note that log level must be equals or higher than <code>
 * Level.FINE</code> if debug is enabled otherwise messages won't be printed.
 * 
 * @version $LastChangedRevision$
 */
public class Logger {

    private static javax.speech.synthesis.Synthesizer speechSynth;

    /**
     * Logger singleton.
     */
    private static java.util.logging.Logger logger;

    /*
     * Configuration.
     *
     * TODO get configuration from environment.
     */
    private static Level logLevel = Level.ALL;
    private static boolean debugEnabled = true;

    // static initialization.
    static {
        try {
            // TODO FIXME make speech api work. I couldn't manage to use it.
            /*
            SynthesizerModeDesc required = new SynthesizerModeDesc();
            //required.setLocale(new Locale("es", null));
            required.addVoice(new Voice());
            speechSynth = javax.speech.Central.createSynthesizer(required);
             */
        } catch(Exception e) {
            Logger.exception("Could not initialize synthesizer.", e);
            speechSynth = null;
        }
        init();
    }

    /**
     * Protected constructor.
     */
    protected Logger() {
    }

    /**
     * Initialize logger with default configuration.
     */
    public static void init() {
        init(logLevel, debugEnabled);
    }

    /**
     * Initialize logger singleton.
     *
     * @param lev Log level.
     * @param isDebug True enables debugging, false disables.
     */
    public static void init(Level lev, boolean isDebug) {
        ConsoleHandler stdOutput = new ConsoleHandler();
        stdOutput.setLevel(logLevel);
        logger = java.util.logging.Logger.getLogger("musiclabs");
        logger.addHandler(stdOutput);
        logger.setLevel(lev);
        debugEnabled = isDebug;
    }

    /**
     * Set log level.
     *
     * @param level
     */
    public static void setLevel(Level level) {
        logLevel = level;
    }

    /**
     * Toggles debug mode on/off.
     *
     * @param isDebug
     */
    public static void setDebug(boolean isDebug) {
        debugEnabled = isDebug;
    }

    /**
     * Generates a type of debug message for exceptions. Messages are printed
     * using log level <code>{@link Level.SEVERE}</code> if debug is enabled.
     *
     * @param msg Log message to be displayed with exception's output.
     * @param e Exception to be logged.
     */
    public static void exception(String msg, Exception e) {
        if(debugEnabled) {
            logger.severe(msg + " : " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * Generates a log of level <code>{@link Level.INFO}</code>.
     *
     * @param msg Log message.
     */
    public static void info(String msg) {
        logger.info(msg);
    }

    /**
     * Generates a log of level <code>{@link Level.WARNING}</code>.
     * 
     * @param msg Log message.
     */
    public static void warning(String msg) {
        logger.warning(msg);
    }

    /**
     * Generates a log of level <code>{@link Level.FINEST}</code>.
     *
     * @param msg Log message.
     */
    public static void trace(String msg) {
        logger.finest(msg);
    }

    /**
     * Generates a log of level <code>{@link Level.FINE}</code>.
     *
     * @param msg Log message.
     */
    public static void debug(String msg) {
        if(debugEnabled) {
            logger.fine(msg);
        }
    }

    /**
     * Speak text string. Please notice that this method is blocking; it'll
     * wait for the synthesizer to finish speaking.
     *
     * @param msg This is the text string to be spoken by the synthesizer.
     */
     /* XXX : not working properly yet.
    public static void speak(String msg) {
        if(speechSynth != null) {
            try {
                speechSynth.speak(msg, null);
                speechSynth.waitEngineState(Synthesizer.QUEUE_EMPTY);
            } catch (Exception ex) {
                Logger.exception("Could not load synthesizer", ex);
            }
        }
    }
    */
}
